<?php
namespace app\hongguan\controller;
use think\Controller;
use think\Db;

class index extends Controller
{
    // 显示红馆主页
    public function index(){
        // 读取导航数据
        $nav = Db::name('nav')->select();
        // 读取banner数据
        $banner = Db::name('banner')->select();
        // 读取最新公告数据
        $noticeList = Db::name('notice')
            ->field('id,title,createtime')
            ->where('type', '=', '1')
            ->order('createtime','desc')
            ->limit(0,5)
            ->select();
        // 读取最新公告视频数据
        $noticeVideo = Db::name('notice')
            ->field('id,title,cover,createtime')
            ->where('type', '=', '2')
            ->order('createtime','desc')
            ->limit(0,1)
            ->find();
        // 处理日期
        $noticeVideo['date'] = date('Y-m', strtotime($noticeVideo['createtime']));
        $noticeVideo['day'] = date('d', strtotime($noticeVideo['createtime']));


        // 读取主题教育
        $theme = Db::name('theme')
            ->field('id,title,image,createtime')
            ->order('createtime','desc')
            ->limit(0,3)
            ->select();
        // $theme['date'] = date('Y-m-d', strtotime($theme['createtime']));
        // 获取社会服务数据
        $social = Db::name('social')
            ->field('id,title,abstract,createtime,image')
            ->order('createtime', 'desc')
            ->limit(0,4)
            ->select();
        // 处理日期时间
        foreach ($social as $k => $v) {
            $social[$k]['date'] = date('Y-m-d', strtotime($social[$k]['createtime']));
        }
        // 获取工作动态数据
        $work = Db::name('work_dynamics')
            ->field('id,title,abstract,createtime')
            ->where('type','=','1')
            ->order('createtime','desc')
            ->limit(0,4)
            ->select();
       foreach ($work as $k => $v){
           $work[$k]['date'] = date('Y-m', strtotime($noticeVideo['createtime']));
           $work[$k]['day'] = date('d', strtotime($noticeVideo['createtime']));
       }
        // 获取工作动态带图片
       $work_image = Db::name('work_dynamics')
           ->field('id,title,image')
           ->order('createtime', 'desc')
           ->where('type', '=', '2')
           ->limit(0, 1)
           ->find();

        // 获取数据库友情链接的数据
        $link = Db::name('link')->select();
        // 红馆展示图
        $show = Db::name('show')->select();
        // 联系方式
        $contact = Db::name('contact')->select();
        // 发送到页面
        // $this->assign('nav', $nav);
        return view('index2', [
            'nav' => $nav,
            'banner' => $banner,
            'noticeList' => $noticeList,
            'noticeVideo' => $noticeVideo,
            'theme' => $theme,
            'work' => $work,
            'link' => $link,
            'social' => $social,
            'show' => $show,
            'work_image' => $work_image,
            'contact' => $contact,
        ]);
    }

    // 显示红馆简介
    public function hgIntroduction(){
        // 接收传输的数据
        $keyword = input('keyword');
        $where = [];
        if (!empty($keyword)){
            $where['title'] = ['like', '%'.$keyword.'%'];
        }
        // 读取导航数据
        $nav = Db::name('nav')->select();
        // 读取banner数据
        $banner = Db::name('banner')->select();
        // 获取社会服务数据(代替红馆简介)
        $social = Db::name('social')
            ->field('id,title,abstract,createtime,image')
            ->where($where)
            ->order('createtime', 'desc')
            ->limit(0,4)
            ->select();
        return view('cardList', [
            'nav' => $nav,
            'banner' => $banner,
            'social' => $social,
        ]);
    }


}